package com.af.robot.controller;

import com.af.robot.config.ConfigThermalData;
import com.af.robot.result.ResultEnum;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
public class DirConfigController {

    @Autowired
    private ConfigThermalData configThermalData;

    @GetMapping("/")
    public String toConfig(HttpServletRequest request){
        if("127.0.0.1".equals(request.getRemoteHost())){
            return "robot/transmitter";
        }
        return "error/404/index";
    }
    @GetMapping("/config")
    public String config(HttpServletRequest request){
        if("127.0.0.1".equals(request.getRemoteHost())){
            return "config/config";
        }
        return "error/404/index";
    }

    @GetMapping("/getPwdInfo")
    @ResponseBody
    public JSONObject getPwdInfo(HttpServletRequest request){
        if("127.0.0.1".equals(request.getRemoteHost())){
            JSONObject result = ResultEnum.OK.getResult();
            result.put("data",configThermalData.getPassFile().getAbsolutePath() + "中的defaultPWD");
            return result;
        }else return ResultEnum.JURISDICTION_ERROR.getResult();
    }

    @PostMapping("/checkPass")
    @ResponseBody
    public JSONObject checkPass(@RequestBody JSONObject object){
        String pass = object.getString("data");
        if (pass.equals(configThermalData.getDefPass())){
            configThermalData.changePWD();
            return ResultEnum.OK.getResult();
        }
        else return ResultEnum.DEFAULT_ERROR.getResult();
    }

    @ResponseBody
    @PostMapping("/saveDirConfig")
    public JSONObject saveConfig(@RequestBody JSONObject config,HttpServletRequest request){
        if("127.0.0.1".equals(request.getRemoteHost())){
            configThermalData.SaveDirConfig(config);
            configThermalData.createLiveFile();
            return ResultEnum.OK.getResult();
        }else return ResultEnum.JURISDICTION_ERROR.getResult();
    }

    @ResponseBody
    @GetMapping("/getDirConfig")
    public JSONObject getDirConfig(HttpServletRequest request){
        if("127.0.0.1".equals(request.getRemoteHost())){
            JSONObject result = ResultEnum.OK.getResult();
            result.put("data",configThermalData.getDirConfig());
            return result;
        }else return ResultEnum.JURISDICTION_ERROR.getResult();
    }

}
